<?php
namespace app\admin\controller;
use app\admin\model\Life as LifeModel;

//大学生活管理类
class Life extends Base
{
    /**
     * 大学生活列表
     */
    public function lifeLst()
    {
        $list = LifeModel::order('sl_create_time DESC')->paginate(10);

        $data = displayJson($list);

        $arr  = json_decode($data,true);

        $userIds   = array();
        $newArr    = $arr['data'];

        foreach ($newArr as $val) {
            $userIds[] = $val['sl_uid'];
        }

        $user_list = $this->getUserEmailByIds($userIds);

        foreach ($newArr as $key => $val) {
            $newArr[$key]['sl_uid']     = $user_list[$val['sl_uid']]['us_email'];
            $newArr[$key]['us_status']  = $user_list[$val['sl_uid']]['us_status'];
        }

        $arr['data'] = $newArr;

        $info['code'] = '200';
        $info['data'] = $arr;
        echo displayJson($info);die;
    }

    /**
     * 获取大学生活详情
     */
    public function getLifeDesc()
    {
        $sl_id = input('sl_id');

        $getLife = db('school_life',[],false)->where('sl_id',$sl_id)->find();

        $uid                  = $getLife['sl_uid'];
        $user                 = $this->getUserEmail($uid);
        $getLife['sl_uid']    = $user[$uid]['us_email'];
        $getLife['us_status'] = $user[$uid]['us_status'];
        $getLife['sl_content'] = strip_tags($getLife['sl_content']);

        $info['code'] = '200';
        $info['data'] = $getLife;
        echo displayJson($info);die;
    }

    /**
     * 删除大学生活
     */
    public function delLife()
    {
        $sl_id = input('sl_id');
        $note  = input('note'); //删除原因

        if (empty($note)) {
            $info['code']    = '400';
            $info['message'] = '删除原因必填';
            echo displayJson($info);die;
        }

        $row   = db('school_life',[],false)->where('sl_id',$sl_id)->find();

        $uid   = $row['sl_uid'];

        $res   = db('school_life',[],false)->where('sl_id',$sl_id)->delete();

        if ($res) {
            //发送邮件告知用户
            $getUser = db('user',[],false)->where('us_id',$uid)->find();

            $send_data = array(
                'to_email'   =>$getUser['us_email'],
                'content'    =>'【淘二货】您发布的大学生活：<span style="font-size: 20px;font-weight: bold;"> '.$row['sl_title'].' </span>，已被管理员删除。<br/>删除原因： '.$note.'<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if ($send) {
                $info['code']    = '200';
                $info['message'] = '删除成功';
                echo displayJson($info);die;
            } else {
                $info['code']    = '400';
                $info['message'] = '删除成功,邮件发送失败';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '删除失败';
            echo displayJson($info);die;
        }
    }
}